package com.ymttest.database.mongo;

import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class YmatouBlacklistUserWapper {
	static MongoDBDriver mdd;

	public YmatouBlacklistUserWapper() throws Exception {
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip"),27017, "Ymt_BlacklistUser_201410");
		mdd.setPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		mdd.auth("authuser", "123456");
		mdd.selectCollection("YmatouBlacklistUser");
	}

	/**
	 * 改变表
	 * 
	 * @param Collection
	 *            表名
	 */
	public void changeCollection(String Collection) {
		mdd.selectCollection(Collection);
	}
	
	
	/**
	 * 从黑名单里删除测试数据，恢复环境
	 * @return
	 * @throws Exception 
	 */
	public boolean deleteData(int uid) throws Exception{
		mdd.selectCollection("YmatouBlacklistUser");
		mdd.delete("{\"_id\":\""+uid+"\"}");
		return true;
	}
	/**
	 * 判段是否有商家IP被锁住记录
	 * @throws Exception 
	 */
	public boolean hasSellerIPLocked(String ip) throws Exception{
		mdd.selectCollection("YmatouBlacklist_Ip_Seller");
		int result = mdd.selectCount("{\"_id\":\""+ip+"\"}");
		return result>0? true:false;
	}
	/**
	 * 判段此IP是否被锁住了
	 * @throws Exception 
	 */
	public boolean isIPLocked(String ip) throws Exception{
		mdd.selectCollection("YmatouBlacklist_Ip");
		int result = mdd.selectCount("{\"_id\":\""+ip+"\"}");
		return result>0? true:false;
	}
	/**
	 * 从黑名单里删除测试数据，恢复环境
	 * @return
	 * @throws Exception 
	 */
	public boolean deleteData(String ip) throws Exception{
		mdd.selectCollection("YmatouBlacklist_Ip");
		mdd.delete("{\"_id\":\""+ip+"\"}");
		return true;
	}
	
	/**
	 * 将Ip加入黑名单
	 * @param ip
	 * @param createTime
	 * @param lastUpdateTime
	 * @param expiredTime
	 * @param allowRequest
	 * @return
	 * @throws Exception 
	 */
	public void saveData(String ip, String createTime, String lastUpdateTime, String expiredTime, boolean allowRequest, int type) throws Exception{
		mdd.selectCollection("YmatouBlacklist_Ip");
		mdd.saveJSON("{\"_id\":\""+ip+"\",\"CreateTime\":{$date:\""+createTime+"\"},\"LastUpdateTime\":{$date:\""+lastUpdateTime+"\"}, "
				+ "\"ExpiredTime\":{$date:\""+expiredTime+"\"},\"AllowRequest\":"+allowRequest+",\"Type\":"+type+"}");
	}
	public void saveSellerData(String ip, String createTime, String lastUpdateTime, String expiredTime, boolean allowRequest, int type) throws Exception{
		mdd.selectCollection("YmatouBlacklist_Ip_Seller");
		mdd.saveJSON("{\"_id\":\""+ip+"\",\"CreateTime\":{$date:\""+createTime+"\"},\"LastUpdateTime\":{$date:\""+lastUpdateTime+"\"}, "
				+ "\"ExpiredTime\":{$date:\""+expiredTime+"\"},\"AllowRequest\":"+allowRequest+",\"Type\":"+type+"}");
	}
	public void updateData(String ip) throws Exception{
		mdd.selectCollection("YmatouBlacklist_Ip");
		String date = YMTDateUtil.getSysTime("yyyy-MM-dd") + "T23:00:00.439Z";
		mdd.update("{\"_id\":\""+ip+"\"}", "{\"ExpiredTime\":\""+date+"\"}");
	}
	public void close() throws Exception{
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set1_mongoip"),27017, "Ymt_BlacklistUser_201410");
		mdd.close();
		mdd=null;
	}
}
